﻿using System;
using System.Data;
using System.Text;
using System.Data.SqlClient;
using IPR_StudyMeetingMrgSys.DBUtility;//Please add references
namespace IPR_StudyMeetingMrgSys.DAL
{
	/// <summary>
	/// 数据访问类:CorpPlan
	/// </summary>
	public partial class CorpPlan
	{
		public CorpPlan()
		{}
		#region  BasicMethod

		/// <summary>
		/// 是否存在该记录
		/// </summary>
		public bool Exists(long AutoID)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("select count(1) from CorpPlan");
			strSql.Append(" where AutoID=@AutoID");
			SqlParameter[] parameters = {
					new SqlParameter("@AutoID", SqlDbType.BigInt)
			};
			parameters[0].Value = AutoID;

			return DbHelperSQL.Exists(strSql.ToString(),parameters);
		}


		/// <summary>
		/// 增加一条数据
		/// </summary>
		public long Add(IPR_StudyMeetingMrgSys.Model.CorpPlan model)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("insert into CorpPlan(");
			strSql.Append("CPID,CPName,CPDescribe,IsRelease,StartDateTime,EndDateTime,DesignUserID,DesignUserName,AddUserID,AddUserName,AddDateTime,DepID,IsCheck,IsUse,AnnexName,AnnexUrl)");
			strSql.Append(" values (");
			strSql.Append("@CPID,@CPName,@CPDescribe,@IsRelease,@StartDateTime,@EndDateTime,@DesignUserID,@DesignUserName,@AddUserID,@AddUserName,@AddDateTime,@DepID,@IsCheck,@IsUse,@AnnexName,@AnnexUrl)");
			strSql.Append(";select @@IDENTITY");
			SqlParameter[] parameters = {
					new SqlParameter("@CPID", SqlDbType.BigInt,8),
					new SqlParameter("@CPName", SqlDbType.NVarChar,100),
					new SqlParameter("@CPDescribe", SqlDbType.VarChar,1000),
					new SqlParameter("@IsRelease", SqlDbType.Int,4),
					new SqlParameter("@StartDateTime", SqlDbType.DateTime),
					new SqlParameter("@EndDateTime", SqlDbType.DateTime),
					new SqlParameter("@DesignUserID", SqlDbType.NVarChar,20),
					new SqlParameter("@DesignUserName", SqlDbType.NVarChar,20),
					new SqlParameter("@AddUserID", SqlDbType.NVarChar,20),
					new SqlParameter("@AddUserName", SqlDbType.NVarChar,20),
					new SqlParameter("@AddDateTime", SqlDbType.DateTime),
					new SqlParameter("@DepID", SqlDbType.Int,4),
					new SqlParameter("@IsCheck", SqlDbType.Int,4),
					new SqlParameter("@IsUse", SqlDbType.Int,4),
                    new SqlParameter("@AnnexName", SqlDbType.NVarChar,100),
                    new SqlParameter("@AnnexUrl", SqlDbType.NVarChar,200)};
			parameters[0].Value = model.CPID;
			parameters[1].Value = model.CPName;
			parameters[2].Value = model.CPDescribe;
			parameters[3].Value = model.IsRelease;
			parameters[4].Value = model.StartDateTime;
			parameters[5].Value = model.EndDateTime;
			parameters[6].Value = model.DesignUserID;
			parameters[7].Value = model.DesignUserName;
			parameters[8].Value = model.AddUserID;
			parameters[9].Value = model.AddUserName;
			parameters[10].Value = model.AddDateTime;
			parameters[11].Value = model.DepID;
			parameters[12].Value = model.IsCheck;
            parameters[13].Value = model.IsUse;
            parameters[14].Value = model.AnnexName;
            parameters[15].Value = model.AnnexUrl;

            object obj = DbHelperSQL.GetSingle(strSql.ToString(),parameters);
			if (obj == null)
			{
				return 0;
			}
			else
			{
				return Convert.ToInt64(obj);
			}
		}
		/// <summary>
		/// 更新一条数据
		/// </summary>
		public bool Update(IPR_StudyMeetingMrgSys.Model.CorpPlan model)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("update CorpPlan set ");
			//strSql.Append("CPID=@CPID,");
			strSql.Append("CPName=@CPName,");
			strSql.Append("CPDescribe=@CPDescribe,");
			//strSql.Append("IsRelease=@IsRelease,");
			strSql.Append("StartDateTime=@StartDateTime,");
			strSql.Append("EndDateTime=@EndDateTime,");
			strSql.Append("DesignUserID=@DesignUserID,");
			strSql.Append("DesignUserName=@DesignUserName,");
            //strSql.Append("AddUserID=@AddUserID,");
            //strSql.Append("AddUserName=@AddUserName,");
            strSql.Append("AddDateTime=@AddDateTime,");
            strSql.Append("AnnexName=@AnnexName,");
            strSql.Append("AnnexUrl=@AnnexUrl");
            //strSql.Append("DepID=@DepID,");
            //strSql.Append("IsCheck=@IsCheck,");
            //strSql.Append("IsUse=@IsUse");
            strSql.Append(" where CPID=@CPID");
			SqlParameter[] parameters = {
					new SqlParameter("@CPID", SqlDbType.BigInt,8),
					new SqlParameter("@CPName", SqlDbType.NVarChar,100),
					new SqlParameter("@CPDescribe", SqlDbType.VarChar,1000),
					new SqlParameter("@IsRelease", SqlDbType.Int,4),
					new SqlParameter("@StartDateTime", SqlDbType.DateTime),
					new SqlParameter("@EndDateTime", SqlDbType.DateTime),
					new SqlParameter("@DesignUserID", SqlDbType.NVarChar,20),
					new SqlParameter("@DesignUserName", SqlDbType.NVarChar,20),
					new SqlParameter("@AddUserID", SqlDbType.NVarChar,20),
					new SqlParameter("@AddUserName", SqlDbType.NVarChar,20),
					new SqlParameter("@AddDateTime", SqlDbType.DateTime),
                    new SqlParameter("@DepID", SqlDbType.Int,4),
                    new SqlParameter("@IsCheck", SqlDbType.Int,4),
                    new SqlParameter("@IsUse", SqlDbType.Int,4),
                    new SqlParameter("@AnnexName", SqlDbType.NVarChar,100),
                    new SqlParameter("@AnnexUrl", SqlDbType.NVarChar,200)
					//new SqlParameter("@AutoID", SqlDbType.BigInt,8)
            };
			parameters[0].Value = model.CPID;
			parameters[1].Value = model.CPName;
			parameters[2].Value = model.CPDescribe;
			parameters[3].Value = model.IsRelease;
			parameters[4].Value = model.StartDateTime;
			parameters[5].Value = model.EndDateTime;
			parameters[6].Value = model.DesignUserID;
			parameters[7].Value = model.DesignUserName;
			parameters[8].Value = model.AddUserID;
			parameters[9].Value = model.AddUserName;
			parameters[10].Value = model.AddDateTime;
			parameters[11].Value = model.DepID;
			parameters[12].Value = model.IsCheck;
            parameters[13].Value = model.IsUse;
            parameters[14].Value = model.AnnexName;
            parameters[15].Value = model.AnnexUrl;
            //parameters[14].Value = model.AutoID;

            int rows=DbHelperSQL.ExecuteSql(strSql.ToString(),parameters);
			if (rows > 0)
			{
				return true;
			}
			else
			{
				return false;
			}
		}

		/// <summary>
		/// 删除一条数据
		/// </summary>
		public bool Delete(long CPID)
		{
			
			StringBuilder strSql=new StringBuilder();
			strSql.Append("update CorpPlan set IsUse=0");
			strSql.Append(" where CPID=@CPID");
			SqlParameter[] parameters = {
					new SqlParameter("@CPID", SqlDbType.BigInt)
			};
			parameters[0].Value = CPID;

			int rows=DbHelperSQL.ExecuteSql(strSql.ToString(),parameters);
			if (rows > 0)
			{
				return true;
			}
			else
			{
				return false;
			}
		}
		/// <summary>
		/// 批量删除数据
		/// </summary>
		public int DeleteList(string CPIDlist)
		{
			StringBuilder strSql=new StringBuilder();


            var listid = "0";
            StringBuilder strSql_select = new StringBuilder();
            strSql_select.Append("select  top 1 CPID from CorpPlan ");
            strSql_select.Append(" where CPID in (" + CPIDlist + ") and IsRelease='0' and IsCheck='0' ");
            DataSet ds = DbHelperSQL.Query(strSql_select.ToString());
            if (ds.Tables[0].Rows.Count > 0)
            {
                for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
                {
                    var item = ds.Tables[0].Rows[i];
                    if (item["CPID"] != null && item["CPID"].ToString() != "")
                    {
                        if (int.Parse(item["CPID"].ToString()) > 0)
                        {
                            listid += "," + item["CPID"].ToString() + "";
                        }
                    }
                }
            }

            strSql.Append(" update CorpPlan set IsUse=0 ");
			strSql.Append(" where CPID in (" + listid + ")  ");
			var rows=DbHelperSQL.ExecuteSql(strSql.ToString());
            return rows;
        }


		/// <summary>
		/// 得到一个对象实体
		/// </summary>
		public IPR_StudyMeetingMrgSys.Model.CorpPlan GetModel(long AutoID)
		{
			
			StringBuilder strSql=new StringBuilder();
			strSql.Append("select  top 1 AutoID,CPID,CPName,CPDescribe,IsRelease,StartDateTime,EndDateTime,DesignUserID,DesignUserName,AddUserID,AddUserName,AddDateTime,DepID,IsCheck,IsUse,AnnexName,AnnexUrl from CorpPlan ");
			strSql.Append(" where AutoID=@AutoID");
			SqlParameter[] parameters = {
					new SqlParameter("@AutoID", SqlDbType.BigInt)
			};
			parameters[0].Value = AutoID;

			IPR_StudyMeetingMrgSys.Model.CorpPlan model=new IPR_StudyMeetingMrgSys.Model.CorpPlan();
			DataSet ds=DbHelperSQL.Query(strSql.ToString(),parameters);
			if(ds.Tables[0].Rows.Count>0)
			{
				return DataRowToModel(ds.Tables[0].Rows[0]);
			}
			else
			{
				return null;
			}
		}


		/// <summary>
		/// 得到一个对象实体
		/// </summary>
		public IPR_StudyMeetingMrgSys.Model.CorpPlan DataRowToModel(DataRow row)
		{
			IPR_StudyMeetingMrgSys.Model.CorpPlan model=new IPR_StudyMeetingMrgSys.Model.CorpPlan();
			if (row != null)
			{
				if(row["AutoID"]!=null && row["AutoID"].ToString()!="")
				{
					model.AutoID=long.Parse(row["AutoID"].ToString());
				}
				if(row["CPID"]!=null && row["CPID"].ToString()!="")
				{
					model.CPID=long.Parse(row["CPID"].ToString());
				}
				if(row["CPName"]!=null)
				{
					model.CPName=row["CPName"].ToString();
				}
				if(row["CPDescribe"]!=null)
				{
					model.CPDescribe=row["CPDescribe"].ToString();
				}
				if(row["IsRelease"]!=null && row["IsRelease"].ToString()!="")
				{
					model.IsRelease=int.Parse(row["IsRelease"].ToString());
				}
				if(row["StartDateTime"]!=null && row["StartDateTime"].ToString()!="")
				{
					model.StartDateTime=DateTime.Parse(row["StartDateTime"].ToString());
				}
				if(row["EndDateTime"]!=null && row["EndDateTime"].ToString()!="")
				{
					model.EndDateTime=DateTime.Parse(row["EndDateTime"].ToString());
				}
				if(row["DesignUserID"]!=null)
				{
					model.DesignUserID=row["DesignUserID"].ToString();
				}
				if(row["DesignUserName"]!=null)
				{
					model.DesignUserName=row["DesignUserName"].ToString();
				}
				if(row["AddUserID"]!=null)
				{
					model.AddUserID=row["AddUserID"].ToString();
				}
				if(row["AddUserName"]!=null)
				{
					model.AddUserName=row["AddUserName"].ToString();
				}
				if(row["AddDateTime"]!=null && row["AddDateTime"].ToString()!="")
				{
					model.AddDateTime=DateTime.Parse(row["AddDateTime"].ToString());
				}
				if(row["DepID"]!=null && row["DepID"].ToString()!="")
				{
					model.DepID=int.Parse(row["DepID"].ToString());
				}
				if(row["IsCheck"]!=null && row["IsCheck"].ToString()!="")
				{
					model.IsCheck=int.Parse(row["IsCheck"].ToString());
				}
				if(row["IsUse"]!=null && row["IsUse"].ToString()!="")
				{
					model.IsUse=int.Parse(row["IsUse"].ToString());
				}
                if (row["AnnexName"] != null)
                {
                    model.AnnexName = row["AnnexName"].ToString();
                }
                if (row["AnnexUrl"] != null)
                {
                    model.AnnexUrl = row["AnnexUrl"].ToString();
                }
            }
			return model;
		}

		/// <summary>
		/// 获得数据列表
		/// </summary>
		public DataSet GetList(string strWhere)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("select AutoID,CPID,CPName,CPDescribe,IsRelease,StartDateTime,EndDateTime,DesignUserID,DesignUserName,AddUserID,AddUserName,AddDateTime,DepID,IsCheck,IsUse,AnnexName,AnnexUrl ");
			strSql.Append(" FROM CorpPlan ");
			if(strWhere.Trim()!="")
			{
				strSql.Append(" where "+strWhere);
			}
			return DbHelperSQL.Query(strSql.ToString());
		}

		/// <summary>
		/// 获得前几行数据
		/// </summary>
		public DataSet GetList(int Top,string strWhere,string filedOrder)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("select ");
			if(Top>0)
			{
				strSql.Append(" top "+Top.ToString());
			}
			strSql.Append(" AutoID,CPID,CPName,CPDescribe,IsRelease,StartDateTime,EndDateTime,DesignUserID,DesignUserName,AddUserID,AddUserName,AddDateTime,DepID,IsCheck,IsUse,AnnexName,AnnexUrl  ");
			strSql.Append(" FROM CorpPlan ");
			if(strWhere.Trim()!="")
			{
				strSql.Append(" where "+strWhere);
			}
			strSql.Append(" order by " + filedOrder);
			return DbHelperSQL.Query(strSql.ToString());
		}

		/// <summary>
		/// 获取记录总数
		/// </summary>
		public int GetRecordCount(string strWhere)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("select count(1) FROM CorpPlan ");
			if(strWhere.Trim()!="")
			{
				strSql.Append(" where "+strWhere);
			}
			object obj = DbHelperSQL.GetSingle(strSql.ToString());
			if (obj == null)
			{
				return 0;
			}
			else
			{
				return Convert.ToInt32(obj);
			}
		}
		/// <summary>
		/// 分页获取数据列表
		/// </summary>
		public DataSet GetListByPage(string strWhere, string orderby, int startIndex, int endIndex)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("SELECT * FROM ( ");
			strSql.Append(" SELECT ROW_NUMBER() OVER (");
			if (!string.IsNullOrEmpty(orderby.Trim()))
			{
				strSql.Append("order by T." + orderby );
			}
			else
			{
				strSql.Append("order by T.AutoID desc");
			}
			strSql.Append(")AS Row, T.*  from CorpPlan T ");
			if (!string.IsNullOrEmpty(strWhere.Trim()))
			{
				strSql.Append(" WHERE " + strWhere);
			}
			strSql.Append(" ) TT");
			strSql.AppendFormat(" WHERE TT.Row between {0} and {1}", startIndex, endIndex);
			return DbHelperSQL.Query(strSql.ToString());
		}

        /*
		/// <summary>
		/// 分页获取数据列表
		/// </summary>
		public DataSet GetList(int PageSize,int PageIndex,string strWhere)
		{
			SqlParameter[] parameters = {
					new SqlParameter("@tblName", SqlDbType.VarChar, 255),
					new SqlParameter("@fldName", SqlDbType.VarChar, 255),
					new SqlParameter("@PageSize", SqlDbType.Int),
					new SqlParameter("@PageIndex", SqlDbType.Int),
					new SqlParameter("@IsReCount", SqlDbType.Bit),
					new SqlParameter("@OrderType", SqlDbType.Bit),
					new SqlParameter("@strWhere", SqlDbType.VarChar,1000),
					};
			parameters[0].Value = "CorpPlan";
			parameters[1].Value = "AutoID";
			parameters[2].Value = PageSize;
			parameters[3].Value = PageIndex;
			parameters[4].Value = 0;
			parameters[5].Value = 0;
			parameters[6].Value = strWhere;	
			return DbHelperSQL.RunProcedure("UP_GetRecordByPage",parameters,"ds");
		}*/

        #endregion  BasicMethod
        #region  ExtensionMethod
        /// <summary>
        /// 是否存在该记录
        /// </summary>
        public bool Exists(string CPName)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select count(1) from CorpPlan");
            strSql.Append(" where CPName=@CPName");
            SqlParameter[] parameters = {
                    new SqlParameter("@CPName", SqlDbType.NVarChar,100)
            };
            parameters[0].Value = CPName;

            return DbHelperSQL.Exists(strSql.ToString(), parameters);
        }
        /// <summary>
        /// 分页获取数据列表
        /// </summary>
        public DataSet GetListByPage(string strWhere, string orderby, int startIndex, int endIndex, out int rowCount)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("SELECT * FROM ( ");
            strSql.Append(" SELECT ROW_NUMBER() OVER (");
            if (!string.IsNullOrEmpty(orderby.Trim()))
            {
                strSql.Append("order by T." + orderby);
            }
            else
            {
                strSql.Append("order by T.AutoID desc");
            }
            strSql.Append(")AS Row, T.*  from CorpPlan T ");
            if (!string.IsNullOrEmpty(strWhere.Trim()))
            {
                strSql.Append(" WHERE " + strWhere);
            }
            strSql.Append(" ) TT");
            rowCount = 0;
            DataSet ds = DbHelperSQL.Query(strSql.ToString());
            if (ds != null)
                if (ds.Tables.Count > 0)
                    if (ds.Tables[0].Rows.Count > 0)
                        rowCount = ds.Tables[0].Rows.Count;

            strSql.AppendFormat(" WHERE TT.Row between {0} and {1}", startIndex, endIndex);
            return DbHelperSQL.Query(strSql.ToString());
        }

        /// <summary>
        /// 更新附件信息把上传分开
        /// </summary>
        public int UpdateAnnex(string CPID, string AnnexName, string AnnexUrl)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append(" update CorpPlan set AnnexName='" + AnnexName + "',AnnexUrl='" + AnnexUrl + "' where CPID=" + CPID);

            return DbHelperSQL.ExecuteSql(strSql.ToString());
        }

        #endregion  ExtensionMethod
    }
}

